package com.lihepeng.leecode.frequency.LinkedList;

import org.junit.Test;

/**
 * 删除链表中的重复元素
 */
public class Solution15 {
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        if (head ==null) {
            return null;
        }
        ListNode dumpyNode = new ListNode(-1);
        dumpyNode.next = head;

        ListNode listnode = dumpyNode;
        while (listnode != null && listnode.next != null) {
            ListNode nextNode = listnode.next;
            if (listnode.val == nextNode.val) {
                // 向下寻找不等于的节点
                while (nextNode!=null && listnode.val ==nextNode.val) {
                    nextNode = nextNode.next;
                }
                listnode.next = nextNode;
            }
            listnode = listnode.next;
        }

        return dumpyNode.next;
    }
    @Test
    public void runTest() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(1);
        node1.next = node2;
        ListNode listNode = deleteDuplicates(node1);
        while (listNode!=null) {
            System.out.print(listNode.val+"--->");
            listNode = listNode.next;
        }
    }
}
